home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / pi-nredir.c.orig < prev    next >
Text File  |  1997-08-05  |  2KB  |  105 lines

  1. /* pi-nredir.c: Redirect a connection over the network
  2.  *
  3.  * Copyright (C) 1997, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "pi-source.h"
  13. #include "pi-socket.h"
  14. #include "pi-dlp.h"
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.   struct sockaddr_in addr2;
  19.   struct pi_sockaddr addr;
  20.   int sd, sd2;
  21.   struct NetSyncInfo N;
  22.   char buffer[0xffff];
  23.   int len;
  24.   int ret;
  25.  
  26.   if (argc < 2) {
  27.     fprintf(stderr,"usage:%s %s\n",argv[0],TTYPrompt);
  28.     exit(2);
  29.   }
  30.  
  31.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  32.     perror("pi_socket");
  33.     exit(1);
  34.   }
  35.     
  36.   addr.pi_family = PI_AF_SLP;
  37.   strcpy(addr.pi_device,argv[1]);
  38.   
  39.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  40.   if(ret == -1) {
  41.     perror("pi_bind");
  42.     exit(1);
  43.   }
  44.  
  45.   ret = pi_listen(sd, 1);
  46.   if(ret == -1) {
  47.     perror("pi_listen");
  48.     exit(1);
  49.   }
  50.  
  51.   sd = pi_accept(sd, 0, 0);
  52.   if(sd < 0) {
  53.     perror("pi_accept");
  54.     exit(1);
  55.   }
  56.   
  57.   if (dlp_ReadNetSyncInfo(sd, &N) < 0) {
  58.     fprintf(stderr, "Unable to read network information, cancelling sync.\n");
  59.     exit(1);
  60.   }
  61.     
  62.   if (!N.lanSync) {
  63.     fprintf(stderr, "LAN Sync not enabled on your PalmPilot, cancelling sync.\n");
  64.     exit(1);
  65.   }
  66.  
  67.   putenv("PILOTLOGFILE=PiDebugNet.log");
  68.   
  69.   sd2 = pi_socket(AF_INET, PI_SOCK_STREAM, 0);
  70.   if (sd2 <0 ) {
  71.     perror("Unable to get socket 2");
  72.     exit(1);
  73.   }
  74.   printf("Got socket 2\n");
  75.   
  76.   memset(&addr2, 0, sizeof(addr2));
  77.   addr2.sin_family = AF_INET;
  78.   addr2.sin_port = htons(14238);
  79.  
  80.   if ((addr2.sin_addr.s_addr = inet_addr(N.hostAddress))==-1) {
  81.     fprintf(stderr, "Unable to parse PC address '%s'\n", N.hostAddress);
  82.     exit(1);
  83.   }
  84.   
  85.   ret = pi_connect(sd2, (struct sockaddr*)&addr2, sizeof(addr2));
  86.  
  87.   if (ret<0) {
  88.     perror("Unable to connect to PC");
  89.     exit(1);
  90.   }
  91.   
  92.   while ((len = pi_read(sd2, buffer, 0xffff))>0) {
  93.     pi_write(sd, buffer, len);
  94.     len = pi_read(sd, buffer, 0xffff);
  95.     if (len < 0)
  96.       break;
  97.     pi_write(sd2, buffer, len);
  98.   }
  99.   
  100.   dlp_AbortSync(sd);
  101.   close(sd2);
  102.  
  103.   exit(0);
  104. }
  105.